package com.yunji.system.dao.impl;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.yunji.common.constant.CacheConstant;
import com.yunji.common.model.domain.SysMenuDO;
import com.yunji.system.dao.ISysMenuDao;
import com.yunji.system.mapper.SysMenuMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author : peter-zhu
 * @date : 2024/11/12 20:00
 * @description : TODO
 **/
@Repository
public class SysMenuDaoImpl implements ISysMenuDao {
    private static final Cache<String, List<SysMenuDO>> SYSTEM_MENU_CACHE = CacheBuilder.newBuilder()
            .expireAfterWrite(CacheConstant.LOCAL_CACHE_EXPIRE, TimeUnit.MINUTES).build();

    @Resource
    private SysMenuMapper sysMenuMapper;

    public List<SysMenuDO> getSysMenuById(String requestUri, String requestType) {
        String key = requestUri + "_" + requestType;
        List<SysMenuDO> list = SYSTEM_MENU_CACHE.getIfPresent(key);
        if (list == null) {
            List<SysMenuDO> menuListByPath = sysMenuMapper.getMenuListByPath(requestUri, requestType);
            SYSTEM_MENU_CACHE.put(key, menuListByPath);
            return menuListByPath;
        }
        return list;
    }

}
